package speed2010.matcher.gui;



import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import speed2010.matcher.lematching.basics.LEAlignment;
import speed2010.matcher.util.OntologyMatcherProperties;

import speed2010.matcher.gui.FOntologyMatcherGUI;
import speed2010.matcher.gui.FRun;
import speed2010.matcher.gui.panels.MainPanel;
import speed2010.matcher.gui.util.MatchingInfo;
import speed2010.matcher.lematching.enums.EMatcher;
import speed2010.matcher.semanticmatching.basics.Alignment;
import speed2010.matcher.semanticmatching.basics.ESSMFunction;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class FLog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3468443159895390820L;
	
	
	private JTabbedPane tabPanelLog;
	private JPanel pnlGeneralInfo;
	private JPanel pnlAcoalign;
	private JTable tblSLAlignment;
	private JTable tblAcoAlignment;
	private JScrollPane scrAcoAlignment;
	private JScrollPane scrSLAlignment;
	private JPanel pnlSLAlignment;
	private JPanel pnlLogList;
	private JTextArea txtLogList;

	public FLog()
	{		
		super(FOntologyMatcherGUI.INSTANCE, "Log", Dialog.ModalityType.APPLICATION_MODAL);
		initialize();
		loadInfo();
	}
	
	private void initialize() {
		
		this.setTitle("Operation's Log");
		this.setSize(606, 402);
		this.setLayout(null);
		this.setResizable(false);
		this.setLocationRelativeTo(FOntologyMatcherGUI.INSTANCE);
		getContentPane().add(getTabPanelLog());		
	}
	
	private void loadInfo() {
		MatchingInfo info = MainPanel.MATCHING_INFO;
		fillSLMatchingTable(info.getSlAlignments());	
		fillAcoTable(info.getAcoAlignments());
		
		String log = "";
		
		if(FRun.SEMANTIC_MATCHING)
		{
			log += "Semantic Matching executed.\n";			
		}
		
		boolean slChecked = false;
		
		if(FRun.FINAL_ALIGN)
		{
			log += this.getSLText();			
			slChecked = true;
			
			log += "Final alignment generated.\n";			
		}
		
		if(FRun.SIMILARITY)
		{
			if(!slChecked)
			{
				log += this.getSLText();			
				slChecked = true;
			}
			
			log += "Similarity calculated using ";
			
			if(OntologyMatcherProperties.SSM_FUNCTION.equals(ESSMFunction.Average))
			{
				log += "Average.";
			}
			else if(OntologyMatcherProperties.SSM_FUNCTION.equals(ESSMFunction.DICE))
			{
				log += "DICE.";
			}
			else if(OntologyMatcherProperties.SSM_FUNCTION.equals(ESSMFunction.Overlap))
			{
				log += "Overlap.";
			}
			log += "\n";
		}
		
		getTxtLogList().setText(log);
		
	}
	
	private String getSLText()
	{
		String log = "";
		if(OntologyMatcherProperties.MATCHER.equals(EMatcher.NONE))
		{
			log += "Structural-linguistic provided by a external file.";
		}
		else
		{
			log += "Structural-linguistic matching executed using ";
			
			if(OntologyMatcherProperties.MATCHER.equals(EMatcher.ALIGNMENT_API))
			{
				log += "AlignmentApi.\n";
			}
			else if(OntologyMatcherProperties.MATCHER.equals(EMatcher.HMATCHCL))
			{
				log += "HMatch.\n";
			}
		}
		return log;
	}
	
	private void fillAcoTable(ArrayList<Alignment> acoAlignments) {
		DefaultTableModel modelo = (DefaultTableModel) tblAcoAlignment.getModel();

		int rowCount = tblAcoAlignment.getRowCount();
		for (int i = 0; i < rowCount; i++) {
			modelo.removeRow(0);
		}

		int cont = 0;
		for (Alignment align : acoAlignments) {
			modelo.addRow(new String[] { "", "" });

			String subject = align.getSubject().getDescription();
			String predicate = align.getPredicate().toString();
			String object = align.getObject().getDescription();
			
			tblAcoAlignment.setValueAt(subject, cont, 0);
			tblAcoAlignment.setValueAt(predicate, cont, 1);
			tblAcoAlignment.setValueAt(object, cont, 2);
			cont++;
		}
		
	}

	private void fillSLMatchingTable(ArrayList<LEAlignment> alignments)
	{
		DefaultTableModel modelo = (DefaultTableModel) tblSLAlignment.getModel();

		int rowCount = tblSLAlignment.getRowCount();
		for (int i = 0; i < rowCount; i++) {
			modelo.removeRow(0);
		}

		int cont = 0;
		for (LEAlignment align : alignments) {
			modelo.addRow(new String[] { "", "" });

			String subject = align.getClass1().getDescription();
			String object = align.getClass2().getDescription();
			double measure = align.getMeasure();
			
			tblSLAlignment.setValueAt(subject, cont, 0);
			tblSLAlignment.setValueAt(object, cont, 1);
			tblSLAlignment.setValueAt(measure, cont, 2);
			cont++;
		}
	}

	private JTabbedPane getTabPanelLog()
	{
		if(tabPanelLog == null)
		{
			tabPanelLog = new JTabbedPane();
			tabPanelLog.setBounds(12, 12, 566, 352);
			tabPanelLog.addTab("Info", null, getPnlGeneralInfo(), null);
			tabPanelLog.addTab("Structural-linguistic alignment", null, getPnlSLAlignment(), null);
			tabPanelLog.addTab("Aco alignment", null, getPnlAcoalign(), null);
		}
		
		return tabPanelLog;
	}
	
	private JPanel getPnlGeneralInfo()
	{
		if(pnlGeneralInfo == null)
		{
			pnlGeneralInfo = new JPanel();
			pnlGeneralInfo.setPreferredSize(new java.awt.Dimension(561, 319));
			pnlGeneralInfo.add(getPnlLogList());
		}
		return pnlGeneralInfo;
	}
	
	private JPanel getPnlSLAlignment()
	{
		if(pnlSLAlignment == null)
		{
			pnlSLAlignment = new JPanel();			
			pnlSLAlignment.setLayout(null);
			pnlSLAlignment.add(getScrSLAlignment());
		}
		return pnlSLAlignment;		
	}
	
	private JPanel getPnlAcoalign() {
		if(pnlAcoalign == null) {
			pnlAcoalign = new JPanel();
			pnlAcoalign.setLayout(null);
			pnlAcoalign.setPreferredSize(new java.awt.Dimension(570, 324));
			pnlAcoalign.add(getScrAcoAlignment());
		}
		return pnlAcoalign;
	}
	
	private JScrollPane getScrSLAlignment() {
		if(scrSLAlignment == null) {
			scrSLAlignment = new JScrollPane();
			scrSLAlignment.setBounds(12, 12, 537, 300);
			scrSLAlignment.setViewportView(getTblSLAlignment());
		}
		return scrSLAlignment;
	}
	
	private JTable getTblSLAlignment() {
		if(tblSLAlignment == null) {
			DefaultTableModel tblSLAlignmentModel = 
				new DefaultTableModel(
						null,
						new String[] { "Ontology 1", "Ontology 2", "Similarity" });
			tblSLAlignment = new JTable();
			tblSLAlignment.setModel(tblSLAlignmentModel);
			
			RowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(tblSLAlignmentModel);    
			tblSLAlignment.setRowSorter(sorter); 
		}
		return tblSLAlignment;
	}
	
	private JScrollPane getScrAcoAlignment() {
		if(scrAcoAlignment == null) {
			scrAcoAlignment = new JScrollPane();
			scrAcoAlignment.setBounds(12, 12, 537, 300);
			scrAcoAlignment.setViewportView(getTblAcoAlignment());
		}
		return scrAcoAlignment;
	}
	
	private JTable getTblAcoAlignment() {
		if(tblAcoAlignment == null) {
			DefaultTableModel tblAcoAlignmentModel = 
				new DefaultTableModel(
						null,
						new String[] { "Ontology 1", "Relation" , "Ontology 2" });
			tblAcoAlignment = new JTable();
			tblAcoAlignment.setModel(tblAcoAlignmentModel);
			
			RowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(tblAcoAlignmentModel);    
			tblAcoAlignment.setRowSorter(sorter); 
		}
		return tblAcoAlignment;
	}
	
	private JPanel getPnlLogList()
	{
		if(pnlLogList == null)
		{
			pnlLogList = new JPanel();
			pnlLogList.setPreferredSize(new java.awt.Dimension(561, 319));
			pnlLogList.add(getTxtLogList());
		}
		return pnlLogList;
	}
	
	private JTextArea getTxtLogList()
	{
		if(txtLogList == null)
		{
			txtLogList = new JTextArea();
			txtLogList.setPreferredSize(new Dimension(500, 250));
			txtLogList.setEditable(false);
			txtLogList.setBackground(Color.WHITE);
		}
		return txtLogList;
	}

}
